package com.livegoods.service;


import com.livegoods.pojo.LivegoodsResult;
import com.livegoods.pojo.LoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.Date;
import java.util.Random;

@Service
public class LoginServiceImpl  implements  LoginService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public LivegoodsResult sendyzm(String phone) {

        //将随机四位数存储到redis
        ValueOperations ops = redisTemplate.opsForValue();
        String randomCode = (String) ops.get(phone);
        if(randomCode!=null){
            return LivegoodsResult.error("亲，验证码3分钟有效，请不要频繁发送验证码!!");
        }

        /**
         * 先从redis通过phone获取对应的随机码
         * 1，有：说明用户上一次发送验证码三分钟以内，告诉用户，请不要频繁发送验证码
         * 2，没有：说明用户验证码过期或者用户没发送过验证码，生成随机码并发送给用户手机
         */


        /*生成随机四位数验证码*/
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <4 ; i++) {
            Random random = new Random();
            int randomNumber = random.nextInt(10);
            sb.append(randomNumber);
        }


        ops.set(phone,sb.toString(), Duration.ofMinutes(3));

        //TODO 同时调用阿里云发送短信api

        LivegoodsResult livegoodsResult = LivegoodsResult.ok(sb.toString());

        return livegoodsResult;
    }

    @Override
    public LivegoodsResult login(String username, String password, HttpServletRequest request) {


        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(username);
        loginLog.setPassword(password);
        //loginLog.setLoginIp(request.getRemoteHost()+request.getRemotePort());
        loginLog.setLoginIp(request.getRemoteAddr());
        System.out.println(request.getRemoteHost()+request.getRemotePort());
        System.out.println(request.getRemoteAddr());
        loginLog.setType(1);
        loginLog.setTime(new Date());


        /**
         * 判断用户的验证码和Redis验证码是否相同
         * 1，验证码过期
         * 2，验证码不同
         * 3，相同
         */
        //获取验证码
        ValueOperations ops = redisTemplate.opsForValue();
        String randomCode = (String) ops.get(username);
        System.out.println("randomCode: "+randomCode);
        //1，验证码过期
        if(randomCode==null){
            loginLog.setMsg("验证码不存在或者过期");
            loginLog.setResult(0);
            //将数据存储到日志集合
            mongoTemplate.save(loginLog);
            return  LivegoodsResult.error("验证码不存在或者过期，请重新获取验证码");
        }

        //2，验证码不同
        if(!randomCode.equals(password)){
            loginLog.setMsg("验证码错误！");
            loginLog.setResult(0);
            //将数据存储到日志集合
            mongoTemplate.save(loginLog);
            return  LivegoodsResult.error("验证码错误！");
        }

        //3，相同
        loginLog.setMsg("登录成功！");
        loginLog.setResult(1);

        //将数据存储到日志集合
        mongoTemplate.save(loginLog);

        return  LivegoodsResult.ok("登录成功!");


    }

    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <4 ; i++) {
            Random random = new Random();
            int randomNumber = random.nextInt(10);
            sb.append(randomNumber);
        }
        System.out.println(sb.toString());

    }
}
